﻿using System;
using System.Net;
using SomeKit.Http;
using SomeKit.Serialization;

namespace SomeKit.TypeSafe
{
    public static partial class ClientHelper
    {
        /// <summary>
        /// Executes the "GET" verb against the resource located at <paramref name="uri"/>.
        /// </summary>
        /// <typeparam name="RESULT">The type of the result parameter.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="handler">The handler receiving the resultant object.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="deserializer">The deserializer.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void Get<RESULT>(this Uri uri, Action<RESULT> handler, Action<Exception> errorHandler, Func<string, RESULT> deserializer, Action<HttpWebRequest> headerHandler = null)
        {
            uri.Get(res => handler(deserializer(res)), errorHandler, headerHandler);
        }
        /// <summary>
        /// Executes the "GET" verb against the resource located at <paramref name="uri"/>. The response is expected to be formatted in XML.
        /// </summary>
        /// <typeparam name="RESULT">The type of the result parameter.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="handler">The handler receiving the resultant object.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void GetXml<RESULT>(this Uri uri, Action<RESULT> handler, Action<Exception> errorHandler, Action<HttpWebRequest> headerHandler = null)
        {
            uri.GetXml(res => handler(res.DeserializeXml<RESULT>()), errorHandler, headerHandler);
        }
        /// <summary>
        /// Executes the "GET" verb against the resource located at <paramref name="uri"/>. The response is expected to be formatted in JSON.
        /// </summary>
        /// <typeparam name="RESULT">The type of the result parameter.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="handler">The handler receiving the resultant object.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void GetJson<RESULT>(this Uri uri, Action<RESULT> handler, Action<Exception> errorHandler, Action<HttpWebRequest> headerHandler = null)
        {
            uri.GetJson(res => handler(res.DeserializeJson<RESULT>()), errorHandler, headerHandler);
        }
    }
}
